/**
 * 
 */
package com.cuc.platform.core.componet.security.service.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cuc.platform.core.componet.security.SecurityFacadeService;
import com.cuc.platform.core.componet.security.dao.SecurityAuthorityDao;
import com.cuc.platform.core.componet.security.domain.SecurityAuthority;
import com.cuc.platform.core.componet.security.service.SecurityAuthorityService;
import com.cuc.platform.core.componet.security.utils.SecurityIdentityTools;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 *
 * 权限服务业务接口实现
 */
public class SecurityAuthorityServiceImpl implements SecurityAuthorityService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#getAllAuths()
	 */
	@Override
	public List<SecurityAuthority> getAllAuths() {
		return securityAuthorityDao.getAllAuths();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#getAuthById(java.lang.String)
	 */
	@Override
	public SecurityAuthority getAuthById(Long id) {
		if (null == id) return null;
		
		return securityAuthorityDao.getAuthById(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#getAuthWithRescs(java.lang.String)
	 */
	@Override
	public SecurityAuthority getAuthWithRescs(Long id) {
		if (null == id) return null;
		
		return securityAuthorityDao.getAuthWithRescs(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#getAuthsByRoleId(java.lang.String)
	 */
	@Override
	public List<SecurityAuthority> getAuthsByRoleId(Long roleId) {
		if (null == roleId) return null;
		
		return securityAuthorityDao.getAuthsByRoleId(roleId);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#insertAuth(com.cuc.platform.core.componet.security.domain.SecurityAuthority)
	 */
	@Override
	public void insertAuth(SecurityAuthority auth) throws ServiceException {
		if (null == auth) return;
		
		/* 赋默认值 */
		//非系统管理级用户
		String isAdmin = auth.getIsAdmin();
		if (null == isAdmin || isAdmin.equals("")){
			auth.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		}
		
		securityAuthorityDao.insertAuth(auth);		
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#updateAuth(com.cuc.platform.core.componet.security.domain.SecurityAuthority)
	 */
	@Override
	public void updateAuth(SecurityAuthority auth) throws ServiceException {
		if ((null == auth) || (null == auth.getId())) return;
		
		/* 赋默认值 */
		//非系统管理级用户
		String isAdmin = auth.getIsAdmin();
		if (null == isAdmin || isAdmin.equals("")){
			auth.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		}
		
		securityAuthorityDao.updateAuth(auth);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#delAuthById(java.lang.String)
	 */
	@Override
	public void delAuth(Long id) throws ServiceException {
		if (null == id) return;
		
		int rv = securityAuthorityDao.delAuthById(id);
		
		//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
		if(rv == 0){
			logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
			throw new ServiceException("系统中不存在要删除的数据！");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#viewRescswithAuthMap(java.lang.String)
	 */
	@Override
	public List<SecurityAuthority> findRescswithAuths(Long authId) {
		if (null == authId) return null;
		
		return securityAuthorityDao.viewRescswithAuths(authId);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityAuthorityService#saveAuthRescUnion(java.lang.String, java.lang.String[])
	 */
	@Override
	public void saveAuthRescUnion(String auth_id, String[] resc_ids)
			throws ServiceException {
		if ((null == auth_id) || auth_id.equals("")) return;
		
		//首先删除该auth_id对应的关联关系，以避免重复数据，然后再循环插入。
		securityAuthorityDao.delAuthRescUnions(Long.valueOf(auth_id));
		
		if ((null == resc_ids) || (resc_ids.equals(""))) return;
		
		/*插入多条关联数据。实际批量数据不多，所以没有采用Mybatis的批量插入方式*/
		//构造参数map
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("auth_id", Long.valueOf(auth_id));
		for(String resc_id : resc_ids){
			map.put("resc_id", Long.valueOf(resc_id));
			securityAuthorityDao.saveAuthRescUnion(map);
		}
		
		//动态刷新关联关系，使权限立即生效
		securityFacadeService.refreshAuthResc();
	}

	//日志
	private static Logger logger = Logger.getLogger(SecurityResourceServiceImpl.class);
	
	/**
	 * Ioc Beans
	 */
	private SecurityAuthorityDao securityAuthorityDao;
	private SecurityFacadeService securityFacadeService;
	/**
	 * @param securityFacadeService the securityFacadeService to set
	 */
	public void setSecurityFacadeService(SecurityFacadeService securityFacadeService) {
		this.securityFacadeService = securityFacadeService;
	}

	/**
	 * @param securityAuthorityDao the securityAuthorityDao to set
	 */
	public void setSecurityAuthorityDao(SecurityAuthorityDao securityAuthorityDao) {
		this.securityAuthorityDao = securityAuthorityDao;
	}
}
